<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/7
 * Time: 15:46
 */

/**
 * 获取某节点下的栏目数组
 * @param int $parent 节点
 * @return mixed
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 */
function get_nav_array($parent = 0)
{
    $nav = \think\Db::name('category')
        ->field('id, parentid, title, other_name, description, thumb, url, type, seo_title, seo_keywords, seo_desc, list_tpl, article_tpl, sort, attr, create_time, update_time')
        ->where('is_show', 1)
        ->order('sort asc, create_time desc')
        ->select();
    foreach ($nav as $key => $value) {
        if (empty($value['url']))
        {
            if ($value['type'] == 1)
            {
                $nav[$key]['url'] = url('home/news/index', ['id'=>$value['id']]);
            } else {
                $nav[$key]['url'] = url('home/info/index', ['id'=>$value['id']]);
            }
        }
    }
    $tree = new \other\Tree($nav);
    $list = $tree->get_tree($parent);
    return $list;
}

/**
 * 获取某个分类下的文章数组
 * @param $category_id 分类id
 * @param string $page 分页数量 默认是0即不分页
 * @param string $attr 文章属性
 * @param string $order 排序
 * @return array
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 */
function get_article_array($category_id, $page = '', $attr = '', $order = 'sort desc')
{
    $pages = '';
    $where['category_id'] = $category_id;
    $where['del_status']  = 0;
    $where['status']      = 1;
    $order_where          = 'is_top desc'.$order;
    if ($page)
    {
        if ($attr)
        {
            $list  = \think\Db::name('article')
                ->field('id, category_id, title, thumb, other_title, keywords, description, author, source, hits, content, sort, photos, enclosure, is_top, tpl, create_time, update_time')
                ->where($where)
                ->where('FIND_IN_SET("'.$attr.'", attr)')
                ->order($order_where)
                ->paginate($page);
            $pages = $list->render();
        } else {
            $list = \think\Db::name('article')
                ->field('id, category_id, title, thumb, other_title, keywords, description, author, source, hits, content, sort, photos, enclosure, is_top, tpl, create_time, update_time')
                ->where($where)
                ->order($order_where)
                ->paginate($page);
            $pages = $list->render();
        }
    } else {
        if ($attr)
        {
            $list = \think\Db::name('article')
                ->field('id, category_id, title, thumb, other_title, keywords, description, author, source, hits, content, sort, photos, enclosure, is_top, tpl, create_time, update_time')
                ->where($where)
                ->where('FIND_IN_SET("'.$attr.'", attr)')
                ->order($order_where)
                ->select();
        } else {
            $list = \think\Db::name('article')
                ->field('id, category_id, title, thumb, other_title, keywords, description, author, source, hits, content, sort, photos, enclosure, is_top, tpl, create_time, update_time')
                ->where($where)
                ->order($order_where)
                ->select();
        }
    }
    return ['list'=>$list, 'pages'=>$pages];
}

/**
 * 获取上一篇文章
 * @param $article_id 当前文章id
 * @param string $attr 属性
 * @param string $order 排序方式
 * @return array|false|PDOStatement|string|\think\Model
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 */
function get_prev_article($article_id, $attr='', $order='sort desc')
{
    $article = \think\Db::name('article')
        ->field('id, category_id')
        ->where('id', $article_id)
        ->find();
    $where['category_id'] = $article['category_id'];
    $where['del_status']  = 0;
    $where['status']      = 1;
    $where['id']          = ['<', $article_id];
    if ($attr)
    {
        $where2           = 'FIND_IN_SET("'.$attr.'", attr)';
    } else {
        $where2           = '1 = 1';
    }
    $prev = \think\Db::name('article')
        ->field('id, category_id, title, thumb, other_title, keywords, description, author, source, hits, content, sort, photos, enclosure, is_top, tpl, create_time, update_time')
        ->where($where)
        ->order('is_top desc, '.$order)
        ->find();
    return !empty($prev) ? $prev : [];
}

/**
 * 获取下一篇文章
 * @param $article_id 当前文章id
 * @param string $attr 属性
 * @param string $order 排序方式
 * @return array|false|PDOStatement|string|\think\Model
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 */
function get_next_article($article_id, $attr='', $order='sort desc')
{
    $article = \think\Db::name('article')
        ->field('id, category_id')
        ->where('id', $article_id)
        ->find();
    $where['category_id'] = $article['category_id'];
    $where['del_status']  = 0;
    $where['status']      = 1;
    $where['id']          = ['>', $article_id];
    if ($attr)
    {
        $where['attr']    = ['exp', 'FIND_IN_SET('.$attr.', attr)'];
    }
    $next = \think\Db::name('article')
        ->field('id, category_id, title, thumb, other_title, keywords, description, author, source, hits, content, sort, photos, enclosure, is_top, tpl, create_time, update_time')
        ->where($where)
        ->order('is_top desc, '.$order)
        ->find();
    return !empty($next) ? $next : [];
}

/**
 * 获取幻灯片
 * @param $cid 幻灯片分类
 * @param int $limit 查询数量
 * @return false|PDOStatement|string|\think\Collection
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 */
function get_banner($cid, $limit=0)
{
    $where['banner_category_id'] = $cid;
    $where['status']             = 1;
    if ($limit)
    {
        $list = \think\Db::name('banner')
            ->field('id, title, thumb, url, create_time, update_time')
            ->where($where)
            ->order('sort desc, create_time desc')
            ->limit($limit)
            ->select();
    } else {
        $list = \think\Db::name('banner')
            ->field('id, title, thumb, url, create_time, update_time')
            ->where($where)
            ->order('sort desc, create_time desc')
            ->select();
    }
    return $list;
}

/**
 * 获取某个栏目的文章
 * @param $category_id 栏目id
 * @param int $limit 获取数量
 * @param string $attr 属性
 * @param string $order 排序
 * @return false|PDOStatement|string|\think\Collection
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 */
function get_articles($category_id, $limit = 0, $attr = '', $order = 'sort desc')
{
    $where['category_id'] = $category_id;
    $where['del_status']  = 0;
    $where['status']      = 1;
    $order_where          = 'is_top desc'.$order;
    if ($limit)
    {
        if ($attr)
        {
            $list  = \think\Db::name('article')
                ->field('id, category_id, title, thumb, other_title, keywords, description, author, source, hits, content, sort, photos, enclosure, is_top, tpl, create_time, update_time')
                ->where($where)
                ->where('FIND_IN_SET("'.$attr.'", attr)')
                ->order($order_where)
                ->limit($limit)
                ->select();
        } else {
            $list = \think\Db::name('article')
                ->field('id, category_id, title, thumb, other_title, keywords, description, author, source, hits, content, sort, photos, enclosure, is_top, tpl, create_time, update_time')
                ->where($where)
                ->order($order_where)
                ->limit($limit)
                ->select();
        }
    } else {
        if ($attr)
        {
            $list = \think\Db::name('article')
                ->field('id, category_id, title, thumb, other_title, keywords, description, author, source, hits, content, sort, photos, enclosure, is_top, tpl, create_time, update_time')
                ->where($where)
                ->where('FIND_IN_SET("'.$attr.'", attr)')
                ->order($order_where)
                ->select();
        } else {
            $list = \think\Db::name('article')
                ->field('id, category_id, title, thumb, other_title, keywords, description, author, source, hits, content, sort, photos, enclosure, is_top, tpl, create_time, update_time')
                ->where($where)
                ->order($order_where)
                ->select();
        }
    }
    return $list;
}

/**
 * 获取面包屑导航
 * @param $category 栏目id
 * @param int $parentid 终止id 默认是0
 * @param array $arr 递归数组
 * @return array
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 */
function get_nav_crumbs($category, $parentid = 0, $arr = array())
{
    $navinfo = \think\Db::name('category')
        ->field('id, parentid, title, other_name, description, thumb, url, type, seo_title, seo_keywords, seo_desc, list_tpl, article_tpl, sort, attr, create_time, update_time')
        ->where('is_show', 1)
        ->where('id', $category)
        ->find();
    if (!empty($navinfo)) {
        if (empty($navinfo['url']))
        {
            if ($navinfo['type'] == 1)
            {
                $navinfo['url'] = url('home/news/index', ['id'=>$navinfo['id']]);
            } else {
                $navinfo['url'] = url('home/info/index', ['id'=>$navinfo['id']]);
            }
        }
        array_unshift($arr, $navinfo);
        if ($navinfo['parentid'] == $parentid) {
            $result = $arr;
        } else {
            $result = get_nav_crumbs($navinfo['parentid'], $parentid, $arr);
        }
    } else {
        $result = array();
    }
    return $result;
}